﻿using System;
using System.Data;
using System.IO;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.PhotoAlbum.Components;

namespace ASPNETVN.PORTAL.Modules.PhotoAlbum.Controls
{
    public partial class AddEditCollection : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnOrganizePicture.Enabled = this.ItemID > 0;
            btnDelete.Enabled = this.ItemID > 0;

            if (this.Page.IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    Components.Collection collection = Components.Collection.Load(this.ItemID);
                    if (collection != null)
                    {
                        txtTitle.Text = collection.Title;
                        chkIsPrivate.Checked = collection.IsPrivate;
                        txtDescription.Text = collection.Description;
                        // Hidden fields.
                        hfCreatedBy.Value = collection.CreatedBy;
                        hfCreationTime.Value = collection.CreationTime.ToString();
                    }
                }
            }
        }

        protected void btnSaveCollection_Click(object sender, EventArgs e)
        {
            Components.Collection collection = new Components.Collection();
            collection.ModuleID = this.ModuleID;
            collection.Title = txtTitle.Text;
            collection.IsPrivate = chkIsPrivate.Checked;
            collection.Description = txtDescription.Text;
            
            
            if (this.ItemID == 0)
            {
                collection.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                collection.CreationTime = DateTime.Now;
                int id = collection.Insert();
                string url = string.Format("~/AddEditPage.aspx?tabindex={0}&tabid={1}&itemid={2}&mid={3}&page=AddEditCollection", this.TabIndex, this.TabID, id, this.ModuleID);
                Response.Redirect(url);
            }
            else
            {
                // Lấy các giá trị từ hidden fields.
                collection.CreatedBy = hfCreatedBy.Value;
                collection.CreationTime = Convert.ToDateTime(hfCreationTime.Value);

                collection.ID = this.ItemID;
                collection.Update();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string uploadPath = this.Settings["UploadPath"].ToString();
            DataSet ds = Picture.SelectBy_CollectionID(this.ItemID);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                // Xóa image.
                string filename = Server.MapPath("~/" + uploadPath + "/PhotoAlbum/" + this.ModuleID + "/" + row["FileName"]);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                // Xóa thumbnail image.
                string filenameThumbnail = Server.MapPath("~/" + uploadPath + "/PhotoAlbum/" + this.ModuleID + "/Thumbnails/" + row["FileName"]);
                if (File.Exists(filenameThumbnail))
                {
                    File.Delete(filenameThumbnail);
                }
            }

            if (Components.Collection.Delete(this.ItemID) > 0)
            {
                string url = string.Format("~/Default.aspx?tabindex={0}&tabid={1}", this.TabIndex, this.TabID);
                Response.Redirect(url);
            }
        }

        protected void btnOrganizePicture_Click(object sender, EventArgs e)
        {
            string url = string.Format("~/AddEditPage.aspx?tabindex={0}&tabid={1}&itemid={2}&mid={3}&page=OrganizePicture", this.TabIndex, this.TabID, this.ItemID, this.ModuleID);
            Response.Redirect(url);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = string.Format("~/Default.aspx?tabindex={0}&tabid={1}", this.TabIndex, this.TabID);
            Response.Redirect(url);
        }
    }
}